//
//  UIAlertController+YCExtension.m
//  YCPublicDemo
//
//  Created by 董知樾 on 2017/5/18.
//  Copyright © 2017年 xinhuanwangluo. All rights reserved.
//

#import "UIAlertController+YCExtension.h"

@implementation UIAlertController (YCExtension)

+ (instancetype)alertControllerWithTitle:(NSString *)title message:(NSString *)message {
    return [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
}
+ (instancetype)actionSheetControllerWithTitle:(NSString *)title message:(NSString *)message {
    return [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
}


- (void)addDefaultAction:(NSString *)title handler:(void (^)(UIAlertAction *))handler {
    [self addAction:[UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:handler]];
}
- (void)addCancelAction:(NSString *)title handler:(void (^)(UIAlertAction *action))handler {
    [self addAction:[UIAlertAction actionWithTitle:title style:UIAlertActionStyleCancel handler:handler]];
}
- (void)addDestructiveAction:(NSString *)title handler:(void (^)(UIAlertAction *action))handler {
    [self addAction:[UIAlertAction actionWithTitle:title style:UIAlertActionStyleDestructive handler:handler]];
}

- (void)addPreferredDefaultAction:(NSString *)title handler:(void (^)(UIAlertAction *action))handler {
    [self addActionWithTitle:title style:UIAlertActionStyleDefault preferred:YES handler:handler];
}
- (void)addPreferredCancelAction:(NSString *)title handler:(void (^)(UIAlertAction *action))handler {
    [self addActionWithTitle:title style:UIAlertActionStyleCancel preferred:YES handler:handler];
}
- (void)addPreferredDestructiveAction:(NSString *)title handler:(void (^)(UIAlertAction *action))handler {
    [self addActionWithTitle:title style:UIAlertActionStyleDestructive preferred:YES handler:handler];
}

- (void)addActionWithTitle:(NSString *)title style:(UIAlertActionStyle)style preferred:(BOOL)preferred handler:(void (^)(UIAlertAction *action))handler {
    UIAlertAction *action = [UIAlertAction actionWithTitle:title style:style handler:handler];
    [self addAction:action];
    if (preferred) {
        self.preferredAction = action;
    }
}


+ (void)alertTitleTip:(NSString *)title {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:nil];
    [alert addDefaultAction:@"确定" handler:nil];
    [alert show];
}

+ (void)alertTipWithTitle:(NSString *)title message:(NSString *)message sure:(void (^)(UIAlertAction *))sure cancel:(void (^)(UIAlertAction *))cancel {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message];
    [alert addDefaultAction:@"确定" handler:sure];
    [alert addCancelAction:@"取消" handler:cancel];
    
    [alert show];
}

- (void)show {
    [[UIApplication sharedApplication].keyWindow.rootViewController showDetailViewController:self sender:nil];
}

@end
